﻿/*
	Place.cs
	Copyright (C) 2012 Keagan Ladds

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Xml;

namespace WeatherPlugin
{
    public class Place
    {
        private string WeatherAPI = "http://www.google.com/ig/api?weather=";
        private int iTempLow;
        private int iTempHigh;
        private int iTempCurrent;
        private Image imgCurrentConditions;
        private string sCurrentConditions;
        private Wind wWindConditions;
        private string sLocation;

        public int LowTemprature
        {
            get { return iTempLow; }
        }

        public int HighTemprature
        {
            get { return iTempHigh; }
        }

        public int CurrentTemprature
        {
            get { return iTempCurrent; }
        }

        public Image CurrentConditionsIcon
        {
            get { return imgCurrentConditions; }
        }

        public string CurrentConditions
        {
            get { return sCurrentConditions; }
        }

        public Wind WindConditions
        {
            get { return wWindConditions; }
        }

        public string Location
        {
            get { return sLocation; }
            set { sLocation = value; }
        }

        public void Update()
        {
            
            XmlDocument weatherdoc = new XmlDocument();
            weatherdoc.Load(WeatherAPI + sLocation);
            XmlElement fNode = (XmlElement)weatherdoc.DocumentElement.FirstChild;
            int ForcastNumber = 0;
            foreach (XmlNode node in fNode.ChildNodes)
            {
                switch (node.Name)
                {
                    case "current_conditions":
                        foreach (XmlNode ccNode in node.ChildNodes)
                        {
                            switch (ccNode.Name)
                            {
                                case "temp_c":
                                    iTempCurrent = Int32.Parse(ccNode.Attributes["data"].Value);
                                    break;

                                case "wind_condition":
                                    wWindConditions = new Wind();
                                    wWindConditions.Parse(ccNode.Attributes["data"].Value);
                                    break;
                            }
                        }
                        break;

                    case "forecast_conditions":
                        if (ForcastNumber == 0)
                        {
                            foreach(XmlNode fcNode in node.ChildNodes)
                            {
                                switch (fcNode.Name)
                                {
                                    case "low":
                                        iTempLow = (Int32.Parse(fcNode.Attributes["data"].Value) - 32) * 5 / 9;
                                        break;
                                    case "high":
                                        iTempHigh = (Int32.Parse(fcNode.Attributes["data"].Value) - 32) * 5 / 9;
                                        break;

                                    case "condition":
                                        sCurrentConditions = fcNode.Attributes["data"].Value;
                                        break;
                                }

                                ForcastNumber++;
                            }
                        }
                        break;
                        
                }
            }
        }
    }


}
